package lv.solcraft.replication.common.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.InputStreamResource;

public class SpringUtils {

    public static GenericApplicationContext createAppContext(String contextFile) {
        try {
            InputStream contextStream = new ByteArrayInputStream(IOUtils.toByteArray(contextFile));
            GenericApplicationContext ctx = new GenericApplicationContext();
            XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
            xmlReader.loadBeanDefinitions(new InputStreamResource(contextStream));
            ctx.refresh();
            return ctx;
        } catch (IOException e) {
            throw new RuntimeException("Error while creating app context", e);
        }
    }
}
